﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Treely.Data.Model;
using Treely.Data.Model.Abstract;

namespace Treely.Data.ViewModels
{
    public class MathQuizTemplateListingViewModel
    {
        public int MathQuizTemplateId { get; set; }

        public string Name { get; set; }

        public int NumberOfSections { get; set; }

        public int TotalQuestions { get; set; }
    }

    public class MathQuizTemplateViewModel
    {
        public int? MathQuizTemplateId { get; set; }

        public IEnumerable<MathQuizTemplateSectionViewModel> Sections { get; set; }
        public string Name { get; set; }
    }

    public class MathQuizTemplateSectionViewModel
    {
        [Key]
        public int? MathQuizTemplateSectionId { get; set; }

        public int? MathQuizTemplateId { get; set; }

        public int? NumberOfQuestions { get; set; }

        public QuestionSubject SectionSubject { get; set; }

        public int MinimumFactor { get; set; }
        public int MaximumFactor { get; set; }
        public int NumberOfFactors { get; set; }
        public AnswerType AnswerType { get; set; }

        public int? NumberOfChoices { get; set; }

        public IEnumerable<MathAnswerable> GenerateSectionQuestions()
        {
            var questions = new List<MathAnswerable>();
            for (var i = 0; i < NumberOfQuestions; i++)
            {
                questions.Add(GenerateQuestion());
            }
            return questions;
        }

        public MathAnswerable GenerateQuestion()
        {
            switch (SectionSubject)
            {
                case QuestionSubject.Addition:
                    return new AdditionQuestion(AnswerType, MinimumFactor, MaximumFactor, NumberOfFactors, NumberOfChoices ?? 0);

                case QuestionSubject.Subtraction:
                    return new SubtractionQuestion(AnswerType, MinimumFactor, MaximumFactor, NumberOfFactors, NumberOfChoices ?? 0);

                case QuestionSubject.Division:
                    return new DivisionQuestion(AnswerType, MinimumFactor, MaximumFactor, NumberOfFactors, NumberOfChoices ?? 0);

                case QuestionSubject.Multiplication:
                    return new MultiplicationQuestion(AnswerType, MinimumFactor, MaximumFactor, NumberOfFactors, NumberOfChoices ?? 0);

                default:
                    throw new ApplicationException(string.Format("Did not set up this question correctly; SectionSubject is {0}", SectionSubject));
            }
        }
    }
}